<div id="mainContent" class="easyui-layout" data-options="fit:true">
<div data-options="region:'north'" style="overflow: hidden;height: 108px;">
        <div class="easyui-panel" data-options="footer:'#footerBtn',border:0">
        <fieldset style="padding: 10px;margin: 4px; border:1px solid #f0ad4e">
            <legend>新增/更新</legend>
                <form id="companyFrm" name="companyFrm" method="POST">
                    <input type="hidden" id="companyId" name="companyId"/>
                    <div style="text-align: right">
                        <input id="companyName" name="companyName" class="easyui-textbox theme-textbox-radius" label="公司名称:" data-options="prompt:'请输入公司名称',required:true" style="width:28%;"/>
                        <input id="companyUUId" name="companyUUId" class="easyui-textbox theme-textbox-radius" label="公司ID:" data-options="prompt:'请输入用字母+数字组合的公司ID',required:true" style="width:30%;"/>
                        <input id="remarks" name="remarks" class="easyui-textbox theme-textbox-radius" label="备注:" data-options="prompt:'请输入公司的备注信息'" style="width:40%;"/>
                    </div>
                    <div id="footerBtn" style="text-align: right;padding-right: 20px;">
                        <a id="submitBtn" href="#" class="easyui-linkbutton" iconCls="icon-ok" style="width:80px;height:26px;">提交</a>
                    </div>
                </form>
        </fieldset>
        </div>
    </div>
    <div data-options="region:'center'">
        <table id="companyDg" title="" class="easyui-datagrid"
               data-options="
                url: '${base}/admin/company/dataList.do',
                fit:true,
                lines: true,
                rownumbers: true,
                striped: true,
                animate:true,
                singleSelect : true,
                showFooter: true,
                pagination: true,
                idField: 'companyId',
                onLoadSuccess: loadSuccess
            " border="0">
            <thead data-options="frozen:true">
            <tr>
                <th data-options="field:'companyName'" width="18%">公司名称</th>
                <th data-options="field:'companyUUId'" width="18%">公司ID</th>
                <th data-options="field:'remarks'" width="20%">备注</th>
                <th data-options="field:'createTime'" width="15%">创建时间</th>
                <th data-options="field:'companyId',formatter:formatterControl" width="18%">操作</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<script>

    var exist = true;
    var isAdd = true;

    $(function(){

        $('#submitBtn').bind('click', function () {
            var msg = '新增';
            if (isAdd) {
                checkCompany($('#companyUUId').val());
                if (exist) {
                    return ;
                }

                $('#companyFrm').attr('action', '${base}/admin/company/addNew.do')
            } else {
                msg = '更新';
                $('#companyFrm').attr('action', '${base}/admin/company/update.do');
            }

            submitFrm('companyFrm', msg+'公司信息', 'companyDg');
            $('#companyFrm').form('clear');
            $('#companyUUId').textbox('enable');

            isAdd = true;
        });

    });

    function checkCompany(companyId) {

        $.ajax({
            type: 'post',
            url:'${base}/admin/company/checkExist.do',
            data:{'cUUId': companyId},
            async : false,
            success:function (data) {
                if(data.ok) {
                    showMsg('操作失败', data.msg);
                    exist = true;
                    return ;
                }
                exist = false;
            }
        });
    }

    function formatterControl(val,row) {
        var edit = '<a class="editBtn" href="#">修改</a>';
        var del = '<a class="delBtn" href="#" ">删除</a>';

        return edit + '&nbsp;&nbsp;&nbsp;&nbsp;' + del;
    }
    
    function loadSuccess(data) {

        var dc = $('#mainContent').find('.editBtn, .delBtn');
        if (dc.length < 1) {
            return;
        }

        $(dc).each(function(r, d){
            var index = Math.floor(r/2);
            var cls = $(this).attr('class');
            var company = data.rows[index];

            if (cls == 'editBtn') {
                $(this).linkbutton({
                    plain: true,
                    iconCls: 'icon-edit',
                    onClick: function () {
                        editCompany(company);
                    }
                });
            } else {
                $(this).linkbutton({
                    plain: true,
                    iconCls: 'icon-remove',
                    onClick: function () {
                        removeCompany(company);
                    }
                });
            }
        });
    }

    function editCompany(data) {
        $('#companyFrm').form('clear');
        $('#companyFrm').form('load', data);
        $('#companyUUId').textbox('disable');
        isAdd = false;
    }

    function removeCompany(company) {
        $.post('${base}/admin/company/delete.do', company, function (data) {
            if(data.ok) {
                showMsg('操作成功', '删除公司信息成功');
                $('#companyDg').datagrid('reload');
            } else {
                showMsg('操作失败', data.msg);
            }
        },'json');
    }
</script>